package alo;

/**
 * 快速排序
 */
public class L_QuickSort {

    public static void main(String[] args) {
        int[] arr = {55,61,3,9, 87,0, 64, 25,53, 14};
        sort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.println(i);
        }

    }

    public static void sort(int[] data, int left, int right) {
        if (left >= right) return;
        int temp = data[left];
        int i = left;
        int j = right;
        while (i != j) {
            //开始一次排序
            while (data[j] >= temp && i < j) j--;//从右往左扫
            if (i < j) data[i] = data[j];
            //找到比基点的数小的值,交换
            while (data[i] <= temp && i < j) i++;//向右扫描
            if (i < j) data[j] = data[i];//交换数据
        }
        //一次排序结束,递归下一次
//        data[left] = data[i];
        data[i] = temp;
        sort(data, left, i - 1);
        sort(data, i + 1, right);
    }

    private static void swap(int[] data, int i, int j) {
        int tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }
}
